package lumis.service.seo.sitemap;


import java.util.ArrayList;
import java.util.Collection;

import lumis.doui.control.text.TextControl;
import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;

/**
 * Text area control whose area is the sitemap xml.
 *
 * @version $Revision: 10825 $ $Date: 2009-08-04 12:36:40 -0300 (Tue, 04 Aug 2009) $
 * @since 5.0.0
 */
public class SitemapXmlTextControl extends TextControl
{
	@SuppressWarnings("unchecked")
	@Override
	protected void setValueFromSource(Source source) throws PortalException
	{
		setReload(false);

		TabularData data = ((TabularSource<?>)source).getData();
		
		//list all channelIds 
		Collection<String> channelIds = new ArrayList<String>(data.size());
		for(ISourceData row: data.getRows())
			channelIds.add(row.get("channelId", String.class));

		//generating the xml structure
		String sitemapStr = SitemapGenerator.createSitemap(source.getSourceContext().getSessionConfig(), 
				channelIds, source.getSourceContext().getTransaction());
		setValue(sitemapStr);
	}
}
